package com.demo.oauth2jFinalShiroClient.web;

import java.util.List;

import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;

import com.demo.oauth2jFinalShiroClient.config.HomeOAuth2Config;
import com.demo.oauth2jFinalShiroClient.config.NeteaseOAuth2Config;
import com.demo.oauth2jFinalShiroClient.config.OAuth2Config;
import com.demo.oauth2jFinalShiroClient.model.Note;
import com.demo.oauth2jFinalShiroClient.oAuth2.ThirdPartToken;
import com.demo.oauth2jFinalShiroClient.oAuth2.ThirdPartUser;
import com.demo.oauth2jFinalShiroClient.oAuth2.TokenHolder;
import com.demo.oauth2jFinalShiroClient.util.CommonResult;
import com.google.gson.Gson;
import com.jfinal.core.Controller;

public class NeteaseResourceController extends Controller {
	
	// url=/netease/userInfo
	public void userInfo(){
		String accessToken = getSessionAttr(HomeOAuth2Config.OAUTH_ACCESS_TOKEN_SESSION_ATTR_NAME);
		OAuth2Config config = (OAuth2Config)getSessionAttr(HomeOAuth2Config.OAUTH_CONFIG_SESSION_ATTR_NAME);
		try {
			OAuthClientRequest userInfoRequest = new OAuthBearerClientRequest(config.getServerUserinfoUrl())
					.setAccessToken(accessToken)
					.buildQueryMessage();
			OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());	            
			OAuthResourceResponse resourceResponse = oAuthClient.resource(userInfoRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
			String jsonStr = resourceResponse.getBody();
			CommonResult<String> commonResult = new Gson().fromJson(jsonStr, CommonResult.class);
			if (commonResult.getSuccess()) {
				String username = commonResult.getData();
				ThirdPartUser thirdPartUser = new ThirdPartUser(username, config.getChannel());
				AuthenticationToken token = new ThirdPartToken(thirdPartUser, accessToken);
				TokenHolder.USER_OAUTH_TOKEN_MAP.put(thirdPartUser, token);
				Subject subject = SecurityUtils.getSubject();
				subject.login(token);
			}
		} catch (Exception e) {
			
		}
		redirect("/index.jsp");
	}
	
	// url=/netease/note
	public void note(){
		CommonResult<List<Note>> result = new CommonResult<>();
		String accessToken = getSessionAttr(HomeOAuth2Config.OAUTH_ACCESS_TOKEN_SESSION_ATTR_NAME);
		OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
		try {
			OAuthClientRequest userInfoRequest = new OAuthBearerClientRequest(NeteaseOAuth2Config.SERVER_LIST_NOTE_URL)
					.setAccessToken(accessToken)
					.buildQueryMessage();
			OAuthResourceResponse resourceResponse = oAuthClient.resource(userInfoRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
			String jsonStr = resourceResponse.getBody();
			result = new Gson().fromJson(jsonStr, CommonResult.class);
		} catch (OAuthSystemException | OAuthProblemException e) {
			e.printStackTrace();
		}
		renderJson(result);
	}
	
}
